﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DLL
{
  public class DataServer
    {
      string Chuoiketnoi = ConfigurationManager.ConnectionStrings["QLCH"].ConnectionString;
        SqlConnection ketnoi = new SqlConnection();
        public DataServer()
        {
            //
            // TODO: Add constructor logic here
            //
        }
      public   void MoKetNoi()
        {
            ketnoi = new SqlConnection(Chuoiketnoi);
            ketnoi.Open();
        }
        void DongketNoi()
        {
            ketnoi.Close();
        }
        public DataTable LayDulieu(string sql, params SqlParameter[] thamso)
        {
            MoKetNoi();
            DataTable bangtrave = new DataTable();
            SqlCommand command = new SqlCommand(sql, ketnoi);
            command.Parameters.AddRange(thamso);
            bangtrave.Load(command.ExecuteReader());

            DongketNoi();
            return bangtrave;
        }
        public void CapNhat(string sql, params SqlParameter[] thamso)
        {
            MoKetNoi();
            SqlCommand command = new SqlCommand(sql, ketnoi);
            command.Parameters.AddRange(thamso);
            command.ExecuteNonQuery();
            DongketNoi();
        }
        public DataTable LayDuLieuProc(string sql, params SqlParameter[] thamso)
        {
            MoKetNoi();
            DataTable bangtrave = new DataTable();
            SqlCommand command = new SqlCommand(sql, ketnoi);
            command.Parameters.AddRange(thamso);
            command.CommandType = CommandType.StoredProcedure;
            bangtrave.Load(command.ExecuteReader());
            DongketNoi();
            return bangtrave;
        }

        public void CapNhatProc(string sql, params SqlParameter[] thamso)
        {
            MoKetNoi();
            SqlCommand command = new SqlCommand(sql, ketnoi);
            command.Parameters.AddRange(thamso);
            command.CommandType = CommandType.StoredProcedure;
            command.ExecuteNonQuery();
            DongketNoi();
        }
    }
}
